﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MySql.Data.MySqlClient;
using LdExpress;
using System.Text.RegularExpressions;

public partial class _Default : System.Web.UI.Page
{
    public string ContainerPage = "./Answers.aspx";
    public string strCondition = "";
    public int NumOfItemsPerPage = 25;
    public string strPager = "";
    public string strError = "";
    public string strMess = "";
    public string type = "";
    public string strProcessName = "";
    public int nPermissions = 0;
    public static string strDisplay = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        int nPage = 1;
        string strPageID = "page_number";

        try
        {

            if (Request.QueryString[strPageID] != null)
                nPage = int.Parse(Request.QueryString[strPageID]);
        }
        catch
        {
            nPage = 1;
        }
        if (nPage <= 0) nPage = 1;

        if ((Session["logined_id"] != null) && (int.Parse(Session["logined_id"].ToString()) > 0))
        {
            if (Session["QuestionAnswer"] != "") nPermissions = int.Parse(Session["QuestionAnswer"].ToString());
            if (Session["superadmin"] != null && int.Parse(Session["superadmin"].ToString()) > 0) nPermissions = 10;
            if (nPermissions <= 0) Response.Redirect("Login.aspx");
        }
        else
            Response.Redirect("Login.aspx");
        //Response.Write(Session["AnswerASK"].ToString() + "--" + strDisplay);
        if (!Page.IsPostBack)
        {
            if (Session["AnswerASK"] != null)
                txtkeyword.Text = Session["AnswerASK"].ToString();
            if (Request.QueryString["action"] == "delete" && nPermissions > 7)
            {
                ContentItem obj = new ContentItem();
                obj.ID = long.Parse(Request.QueryString["ID"]);
                obj.Load(obj.ID, GV.cn, GV.trans);
                obj.Delete(GV.cn, GV.trans);
                Response.Redirect("answers.aspx?" + ((Request.QueryString["page_number"] != null && Request.QueryString["page_number"].ToString() != "") ? "page_number=" + Request.QueryString["page_number"].ToString() : ""));
            }

            #region Build List
            int nNumOfPages = 0;

            DataTable dtList;
            BuildList(NumOfItemsPerPage, nPage, out nNumOfPages, out dtList, null, null);
            if (nPage > nNumOfPages) nPage = 1;

            rptData.DataSource = dtList;
            rptData.DataBind();
            #endregion
            #region Build Pager
            CommonFunctions.BuildPager(Request, nNumOfPages, nPage, strPageID, ContainerPage, out strPager);
            #endregion
        }

    }

    public string DoAction(object ID)
    {
        string strAction = "";
        if (nPermissions >= 3)
            strAction = "<a href=\"Answers.aspx?type=" + type + "&ID=" + ID.ToString() + ((Request.QueryString["page_number"] != null && Request.QueryString["page_number"].ToString() != "") ? "&page_number=" + Request.QueryString["page_number"].ToString() : "") + "\">Edit</a>";
        if (nPermissions > 7)
            strAction += " - <a href=\"javascript:doDelete('" + System.Web.HttpUtility.HtmlEncode(ID.ToString()) + "')\">" + "Delete</a>";
        return strAction;
    }
    private void BuildList(int NumOfItemsPerPage, int PageNo, out int nNumOfPages, out DataTable dtList, MySqlConnection cn, MySqlTransaction trans)
    {
        DataSet ds = null;
        dtList = new DataTable();
        dtList.Columns.Add("ID");
        dtList.Columns.Add("Index");
        dtList.Columns.Add("Summary");//Ask 
        dtList.Columns.Add("Detail");//Answer - Detail
        dtList.Columns.Add("AskName");//Create By ID - CreateName
        dtList.Columns.Add("AnswerName");//Posted By ID - PostedName
        dtList.Columns.Add("LastUpdate");
        dtList.Columns.Add("Action");
        //Get List by SoIDs
        nNumOfPages = 0;

        ContentItem obj = new ContentItem();
        if (Session["AnswerASK"] != null && Session["AnswerASK"].ToString() != "")
            strCondition = " AND (summary like '%" + txtkeyword.Text.Trim() + "%' OR Detail like '%" + Session["AnswerASK"].ToString() + "%')";
        obj.GetList(strCondition, " ID ASC", NumOfItemsPerPage, PageNo, out nNumOfPages, out ds, GV.cn, GV.trans);

        string strAllQueryString = "";
        foreach (string strKey in Request.QueryString.AllKeys)
        {
            if (strKey != null)
            {
                if ((strKey.ToLower() != "action") && (strKey.ToLower() != "id"))
                    strAllQueryString = strAllQueryString + "&" + strKey + "=" + Request.QueryString[strKey].ToString();
            }
        }

        if (strAllQueryString.Length >= 1)
            strAllQueryString = strAllQueryString.Substring(1);

        if (ds != null)
        {
            int count = 0;
            if (Request.QueryString["page_number"] != null && Request.QueryString["page_number"].ToString() != "")
            {
                int pagenumber = 1;
                try
                {
                    pagenumber = int.Parse(Request.QueryString["page_number"]);
                    count = NumOfItemsPerPage * (pagenumber - 1);
                }
                catch { count = 0; }
            }
            foreach (DataRow dr in ds.Tables[0].Rows)
            {

                count++;
                string strID = dr["ID"].ToString();
                //string strIndex = count.ToString();
                string strIndex = dr["ID"].ToString();
                string strName = dr["Summary"].ToString();
                //if (nPermissions >= 3)
                //    strName = "<a href=\"Answers.aspx?ID=" + dr["ID"].ToString() + ((Request.QueryString["page_number"] != null && Request.QueryString["page_number"].ToString() != "") ? "&page_number=" + Request.QueryString["page_number"].ToString() : "") + "\">" + strName + "</a>";
                string strDetail = dr["Detail"].ToString();
                string strAskName = dr["CreateName"].ToString();
                string strAnswerName = dr["PostedName"].ToString();
                string strLastUpdate = (DateTime.Parse(dr["LastUpdatedDate"].ToString())).ToString("dd/MM/yyyy");
                string strAction = "";
                if (nPermissions > 9 || (nPermissions <= 9 && strDetail.Trim().Length == 0 && Session["logined_id"].ToString() == dr["CreatedByID"].ToString()))
                    strAction = "<a href=\"javascript:doEdit('" + dr["ID"].ToString() + "')\">Edit</a>";
                if (nPermissions > 7)
                    strAction += " - <a href=\"javascript:doDelete('" + strID + "')\">" + "Delete</a>";

                string[] strValues = new string[] { strID, strIndex, strName, strDetail, strAskName, strAnswerName, strLastUpdate, strAction };
                dtList.Rows.Add(strValues);
            }
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        strCondition = "";
        Session["AnswerASK"] = txtkeyword.Text.Trim();
        if (txtkeyword.Text.Trim().Length > 0)
        {
            strDisplay = "style='display:block;'";
        }
        else
            strDisplay = "";
        Response.Redirect("Answers.aspx");
    }
}
